package com.chen.pspms.service.impl;

import com.chen.pspms.entity.Majorhazardregistertable;
import com.chen.pspms.mapper.MajorhazardregisterMapper;
import com.chen.pspms.service.CompanyService;
import com.chen.pspms.service.MajorhazardregisterService;
import com.chen.pspms.service.StandardizationService;
import com.chen.pspms.utils.ApplicationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Iterator;
import java.util.List;

@Service
@Transactional
public class MajorhazardregisterServiceImpl implements MajorhazardregisterService {
    @Autowired
    MajorhazardregisterMapper majorhazardregisterMapper;
    @Autowired
    CompanyService companyService;
    @Autowired
    StandardizationService standardizationService;


    @Override
    public Integer save(Majorhazardregistertable majorhazardregistertable) throws ApplicationException {
        int row=0;
        try {
            row = majorhazardregisterMapper.save(majorhazardregistertable);
        } catch (Exception e) {
            throw new ApplicationException("新增重大危险源注册登记表失败", e);
        }
        return row;
    }

    @Override
    public Integer delete(Integer id) throws ApplicationException {
        int row=0;
        try {
            row = majorhazardregisterMapper.delete(id);
        } catch (Exception e) {
            throw new ApplicationException("删除重大危险源注册登记表失败", e);
        }
        return row;
    }

    @Override
    public Majorhazardregistertable findyById(Integer id) throws ApplicationException {
        Majorhazardregistertable majorhazardregistertable;
        try {
            majorhazardregistertable = majorhazardregisterMapper.findyById(id);
            //查找并设置企业，生产标准化
            majorhazardregistertable.setCompany(companyService.findyById(majorhazardregistertable.getCompany().getId()));
            majorhazardregistertable.setStandardizationtable(
                    standardizationService.findyById(majorhazardregistertable.getStandardizationtable().getId()));
        } catch (Exception e) {
            throw new ApplicationException("查询重大危险源注册登记表失败", e);
        }
        return majorhazardregistertable;
    }

    @Override
    public Integer update(Majorhazardregistertable majorhazardregistertable) throws ApplicationException {
        int row=0;
        try {
            row = majorhazardregisterMapper.update(majorhazardregistertable);
        } catch (Exception e) {
            throw new ApplicationException("更新重大危险源注册登记表失败", e);
        }
        return row;
    }

    @Override
    public List<Majorhazardregistertable> findyAll() throws ApplicationException {
        List<Majorhazardregistertable> majorhazardregistertablelist;
        try {
            majorhazardregistertablelist = majorhazardregisterMapper.findyAll();
            for(Majorhazardregistertable majorhazardregistertable:majorhazardregistertablelist){
                majorhazardregistertable.setCompany(companyService.findyById(majorhazardregistertable.getCompany().getId()));
                majorhazardregistertable.setStandardizationtable(
                        standardizationService.findyById(majorhazardregistertable.getStandardizationtable().getId()));
            }
        } catch (Exception e) {
            throw new ApplicationException("查询重大危险源注册登记列表失败", e);
        }
        return majorhazardregistertablelist;
    }

    @Override
    public List<Majorhazardregistertable> findyListByComId(Integer id) throws ApplicationException {
        List<Majorhazardregistertable> majorhazardregistertablelist;
        try {
            majorhazardregistertablelist = majorhazardregisterMapper.findyListByComId(id);
            for(Majorhazardregistertable majorhazardregistertable:majorhazardregistertablelist){
                majorhazardregistertable.setCompany(companyService.findyById(majorhazardregistertable.getCompany().getId()));
                majorhazardregistertable.setStandardizationtable(
                        standardizationService.findyById(majorhazardregistertable.getStandardizationtable().getId()));
            }
        } catch (Exception e) {
            throw new ApplicationException("通过企业查询重大危险源注册登记列表失败", e);
        }
        return majorhazardregistertablelist;
    }

    @Override
    public List<Majorhazardregistertable> removeSafe(List<Majorhazardregistertable> majorhazardregisterList) {
        //移除等级为 无 的
        Iterator<Majorhazardregistertable> iterator=majorhazardregisterList.iterator();
//       for(Majorhazardregistertable majorhazardregistertable:majorhazardregisterList){
//           if(majorhazardregistertable.getLevel().equals("无")){
//               majorhazardregisterList.remove(majorhazardregistertable);
//           }
//       }
        while(iterator.hasNext()){
            if(iterator.next().getLevel().equals("无")){
                iterator.remove();
            }
        }
        return majorhazardregisterList;
    }
}
